﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Common;

public partial class website_admin_knowlegebase_contenteditor : PageBase
{
    public string Title = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        /// 保存设置
        if (RequestBase.GetFormString("____EVENTTARGET") == "save")
        {
            Save();
        }
 
        if (!IsPostBack)
        {
            Initialize();
        }
    }

    #region 数据初始化
    void Initialize()
    {
        if (RequestBase.GetQueryString("c_number") != "")
        {
            DataTable dat = BLL.Search.CommonSearch.ExecSelect("KbCategoryBase", new string[] { "CategoryNumber" }, new string[] { "Equel" }, new string[] { "'" + RequestBase.GetQueryString("c_number") + "'" }, null, null);
            if (dat != null && dat.Rows.Count > 0)
            {
                txtCategoryId.Value = dat.Rows[0]["CategoryId"].ToString().Trim();
                txtTitle.Value = dat.Rows[0]["CategoryName"].ToString().Trim();
                Title = txtTitle.Value;

                object _articleid = BLL.Search.CommonSearch.ExecScalar("KbArticleBase", "TOP 1 ArticleId", new string[] { "CategoryId" }, new string[] { "Equel" }, new string[] { "'" + txtCategoryId.Value + "'" }, null, null);
                if (_articleid != null)
                {
                    txtArticleId.Value = _articleid.ToString();
                    Model.KbArticleBase _obj = BLL.KbArticleBase.GetObjById(txtArticleId.Value.Trim());
                    ArticleObj = _obj;
                }
            }
        }
    }

    Model.KbArticleBase ArticleObj
    {
        get
        {
            Model.KbArticleBase _article = new Model.KbArticleBase();

            if (txtArticleId.Value.Trim() != "")
                _article.ArticleId = txtArticleId.Value.Trim();

            _article.Title = txtTitle.Value.Trim();
            _article.CategoryId = txtCategoryId.Value;
            _article.Content = txtContent.Text;
            _article.PublishDate = DateTime.Now;

            _article.ModifiedOn = DateTime.Now;
            _article.CreatedOn = DateTime.Now;
            _article.ModifiedBy = this.SystemUserId;
            _article.CreatedBy = this.SystemUserId;

            return _article;
        }
        set
        {
            if (value != null)
            {
                txtTitle.Value = value.Title.Trim(); 
                txtContent.Text = value.Content;
                txtCategoryId.Value = value.CategoryId; 
            }
        }
    }
    #endregion
    #region 保存数据
    void Save()
    {
        string res = "";

        if (txtArticleId.Value.Trim() == "")
        {
            res = BLL.KbArticleBase.Add(ArticleObj);
        }
        else
        {
            res = BLL.KbArticleBase.Update(ArticleObj);
        }

        switch (res.Split('|')[0])
        {
            case "succeeded":
                JScript.AlertAndReturnValueToParent("信息保存成功!", "window.location.href=window.location.href;");
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}
